package com.css.fxfzypg.modules.earthquakedisasterloss.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.earthquakedisasterloss.service.PopulationCountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.Map;

@RestController
@RequestMapping("/count")
@Slf4j
public class PopulationCountController {

    @Resource
    private PopulationCountService populationCountService;

    //模板
    @PostMapping("/AzCount")
    public RestResponse AzCount(@RequestParam String taskId, @RequestParam String modelId) {
        RestResponse restResponse = null;
        try {
            restResponse = populationCountService.AzCount(modelId, taskId);
            if (restResponse.getCode()==200) {
                restResponse.setMessage("计算成功!");
            }
        } catch (Exception e) {
            restResponse = RestResponse.fail(503,"计算失败,请检查参数");
            e.printStackTrace();
        }
        return restResponse;
    }

    /**
     * 计算区域下安置人数
     *
     * @param taskId
     * @return
     */
    @PostMapping("/calculate")
    public RestResponse calculate(@RequestParam String taskId,@RequestParam String range) {
        RestResponse restResponse = null;
        try {
            restResponse = populationCountService.saveResult(taskId,range);
            if (restResponse.getCode()==200) {
                restResponse.setMessage("计算成功!");
            }
        } catch (Exception e) {
            restResponse = RestResponse.fail(503,"计算失败,请检查参数");
            e.printStackTrace();
        }
        return restResponse;
    }

    //    公式
    @PostMapping("/AzMath")
    public RestResponse AzMath(@RequestBody Map<String,Object> map){
        RestResponse restResponse = null;
        try {
            restResponse = populationCountService.AzMath((String) map.get("taskId"),(String) map.get("tab"),(String) map.get("range"));
            if (restResponse.getCode()==200) {
                restResponse.setMessage("计算成功!");
            }
        } catch (Exception e) {
            restResponse = RestResponse.fail(503,"计算失败,请检查参数");
            e.printStackTrace();
        }
        return restResponse;
    }

    /**
     * 导出各烈度下安置人数
     * @param
     * @return
     */
    @GetMapping("/exportAzrs")
    public void exportAzrs(String taskId, HttpServletResponse resp){
        try {
            //  return casualtyService.countIntensityAndStructureOfArea((String) map.get("taskId"),(String) map.get("tab"),(String) map.get("range"));
            populationCountService.exportAzrs(taskId,resp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出各烈度下紧急安置人数数据失败");
        }
    }

    /**
     * @author Dongbj
     * @date 2022/11/28 11:01
     * @desc 评估结果紧急安置人员分布图
     */
    @GetMapping("/getPbeByTaskIdAndIntensity")
    public RestResponse getPbeByTaskIdAndIntensity(@NotBlank(message = "任务id不能为空") @RequestParam String taskId,
                                                   @NotBlank(message = "地震烈度不能为空") @RequestParam String dzIntensity) {
        RestResponse restResponse = null;
        try {
            restResponse = populationCountService.getPbeByTaskIdAndIntensity(taskId, dzIntensity);
        } catch (Exception e) {
            restResponse = RestResponse.fail(503,"计算失败,请检查参数");
            e.printStackTrace();
        }
        return restResponse;
    }

}
